package com.xt.web;

import com.xt.dto.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Random;


/**
 * Created by Administrator on 2017/7/6.
 */
@RestController
public class HelloController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index() throws InterruptedException {
        logger.info("into /hello");
        ServiceInstance instance = client.getLocalServiceInstance();
        int sleepTime = new Random().nextInt(3000);
        logger.info("sleepTime:"+sleepTime);
        Thread.sleep(sleepTime);
        logger.info("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
        return "hello world";
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET, produces="application/json;charset=UTF-8")
    public User user(Integer userId){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/user,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
        return new User();
    }

    @RequestMapping(value = "/hello1",method = RequestMethod.GET, produces="application/json;charset=UTF-8")
    public String hello(String name){
        return "hello "+name;
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.GET, produces="application/json;charset=UTF-8")
    public User hello(String name,Integer age){
        return new User(name,age);
    }

    @RequestMapping(value = "/hello3",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
    public String hello(User user){
        return "hello "+user.getUserName()+","+user.getAge();
    }
}
